package test.thread.tmp;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class ConditionExample2 {

    Lock lock = new ReentrantLock();
    Condition condition = lock.newCondition();
    public static AtomicLong luckyOne = new AtomicLong(0);
    public static AtomicBoolean atomicBoolean = new AtomicBoolean(false);

    String getToken() throws InterruptedException {
        if (!hasToken()) {
            requestToken();
        }
        return "token";
    }

    private void requestToken() {
        long num = luckyOne.getAndIncrement();
        if (num != 1) {
            waitGet();
        }
        if (num == 1) {
            httpGet();
        }

    }

    private void httpGet() {
        try {
            lock.lock();
            System.out.println("请求---");
            Thread.sleep(2000);
            atomicBoolean.set(true);
            System.out.println("获取到");
            luckyOne.set(0);
            condition.signalAll();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    private void waitGet() {
        try {
            lock.lock();
            System.out.println("等待");
            condition.await();
            System.out.println("执行下面的步骤");
        } catch (InterruptedException e) {
        } finally {
            lock.unlock();
        }
    }

    private boolean hasToken() {
        return atomicBoolean.get();
    }

    public static void main(String[] args) {

        ExecutorService exec = Executors.newCachedThreadPool();
        final ConditionExample2 conditionExample2 = new ConditionExample2();
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    System.out.println(conditionExample2.getToken());
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    System.out.println(conditionExample2.getToken());
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    System.out.println(conditionExample2.getToken());
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    System.out.println(conditionExample2.getToken());
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    System.out.println(conditionExample2.getToken());
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }).start();
        // for (int i = 0; i < 10; i++) {
        // exec.execute(new Runnable() {
        // @Override
        // public void run() {
        //
        // try {
        // System.out.println(conditionExample2.getToken());
        // } catch (InterruptedException e) {
        // // TODO Auto-generated catch block
        // e.printStackTrace();
        // }
        // }
        // });
        // }
        //
        // exec.shutdown();
    }
}
